package com.xinziruo.richclient.util.filecommand.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.xinziruo.richclient.util.ExceptionUtils;
import com.xinziruo.richclient.util.IOUtils;
import com.xinziruo.richclient.util.filecommand.FileCommand;

/**
 * 
 * 
 * @author loudyn.
 */
public class WriteFileToCommand implements FileCommand {
	private final File source;
	private final OutputStream out;

	public WriteFileToCommand(File source, OutputStream out) {
		this.source = source;
		this.out = out;
	}

	/*
	 * (non-Javadoc)
	 * @see com.youboy.module.util.filecommand.FileCommand#execute()
	 */
	public void execute() {
		InputStream in = null;

		try {
			
			in = new FileInputStream(source);
			IOUtils.piping(in, out);
		} catch (Exception e) {
			throw ExceptionUtils.toUnchecked(e);
		} finally {
			IOUtils.freeQuietly(in, out);
		}
	}
}
